// Learn TypeScript:
//  - https://docs.cocos.com/creator/manual/en/scripting/typescript.html
// Learn Attribute:
//  - https://docs.cocos.com/creator/manual/en/scripting/reference/attributes.html
// Learn life-cycle callbacks:
//  - https://docs.cocos.com/creator/manual/en/scripting/life-cycle-callbacks.html

/**
 *     帧动画 类
 */
import qrj_tools from "./qrj_tools";
const { ccclass, property } = cc._decorator;
@ccclass
export default class FrameAnimation extends cc.Component {

    @property
    is_play: boolean = true;

    @property // 动画速度 (帧/秒)
    speed: number = 24;

    @property([cc.SpriteFrame])
    img_array: cc.SpriteFrame[] = []; // 帧动画顺序 (图片文件名数组)

    @property() // 不需要写方括号 , 数字间以逗号间隔开
    strFrameIdArray: string = ""; // 帧ID 数组 , 不填默认从头到尾

    @property
    loop: number = -1; // 是否循环

    @property
    is_auto_free: boolean = false; // 如果不循环,播放结束释放资源,否则卡在最后一帧

    _jiange: number = 0;
    _frame_id_array: number[] = null;
    _sprfra: cc.Sprite = null;
    _curr_frame_index = 0; // 当前帧索引 (_frame_id_array数组)
    _loop_count = 0;

    onLoad() {

    }
    start() {
        if (this.getComponent(cc.Sprite) == null) { // 如果节点没有 精灵组件
            this.addComponent(cc.Sprite);
        }
        this._sprfra = this.getComponent(cc.Sprite);
        this._init_frame_arrar();
        this._init_jiange();
        this.set_curr_frame();
    }

    onEnable() {
        this._curr_frame_index = 0; // 当前帧索引 (_frame_id_array数组)
        this._loop_count = 0;
        this._use_time = 0;
    }

    play() {
        this.is_play = true;
    }
    stop() {
        this.is_play = false;
    }

    replay() {
        this._curr_frame_index = 0; // 当前帧索引 (_frame_id_array数组)
        this._loop_count = 0;
        this._use_time = 0;
        this.play();
        if (this._sprfra != null) {
            this.set_curr_frame();
        }
    }

    /**
     *     动画 间隔时间
     */
    _init_jiange() {
        if (this.speed > 60) {
            this.speed = 60;
        } else if (this.speed <= 0) {
            this.speed = 1;
        }
        this._jiange = 1 / this.speed;
    }

    /**
     *     设置动画 速度
     */
    set_speed(n: number) {
        this.speed = n;
        this._init_jiange();
    }

    /**
     *     设置 动画 结束 事件
     */
    _end_callback = null;
    set_end_event(cb) {
        this._end_callback = cb;
    }

    //     初始化 帧 ID 数组
    _init_frame_arrar() {
        if (this._frame_id_array != null) {
            return;
        }

        // 没有填写  帧数据
        if (this.strFrameIdArray == null || this.strFrameIdArray.length == 0) {
            let len = this.img_array.length;
            this._frame_id_array = [];
            for (let i = 0; i < len; i++) {
                this._frame_id_array.push(i);
            }
        } else {
            let str = "[" + this.strFrameIdArray + "]";
            let t = qrj_tools.json_to_obj(str);
            if (t == null) { // 填写的帧ID数组有问题
                this.strFrameIdArray = null;
                this._init_frame_arrar();
            } else {
                this._frame_id_array = t;
            }
        }
    }

    /**
     *    设置 当前 帧 , 根据_curr_frame_index
     */
    set_curr_frame() {
        this._init_frame_arrar();

        let ind_img = this._frame_id_array[this._curr_frame_index];
        if (ind_img < 0 || ind_img >= this.img_array.length || this.img_array[ind_img] == null) {
            this._sprfra.spriteFrame = null;
        } else {
            this._sprfra.spriteFrame = this.img_array[ind_img];
        }
    }
    
    _use_time = 0;
    update(dt) {
        if (this.is_play == false) {
            return;
        }

        this._use_time += dt;
        if (this._use_time >= this._jiange) {
            this._use_time -= this._jiange;
            if (this._use_time >= this._jiange) {
                this._use_time = 0;
            }

            if (this.loop == -1) {
                if (++this._curr_frame_index >= this._frame_id_array.length) {
                    this._curr_frame_index = 0;
                }
                this.set_curr_frame();
            } else {
                if (++this._curr_frame_index >= this._frame_id_array.length) { //帧结尾
                    if (++this._loop_count >= this.loop) { // 循环结束
                        if (this.is_auto_free == true) { // 如果 自动释放
                            this.node.removeFromParent();
                        } else {
                            this._curr_frame_index = this._frame_id_array.length - 1;
                            this._loop_count = this.loop;

                            if (this._end_callback != null) { //动画结束动画
                                this._end_callback();
                            }
                        }
                    } else {
                        this._curr_frame_index = 0;
                    }
                }
                this.set_curr_frame();
            }
        }
    }

    /***********************************************
     *             全局工厂 函数
     ***********************************************/
    //  代码 创建 动画
    public static crea_frame_animation(img: cc.SpriteFrame[], strFraIdArr: string = ""): FrameAnimation {
        let n = new cc.Node();
        n.addComponent(cc.Sprite);
        n.addComponent(FrameAnimation);
        let fa = n.getComponent(FrameAnimation);
        fa.img_array = img;
        fa.strFrameIdArray = strFraIdArr;
        return fa;
    }

    //  根据 图集 创建动画
    public static crea_frame_animation_with_atlas(img: cc.SpriteAtlas, strFraIdArr: string = ""): FrameAnimation {
        let imgs: cc.SpriteFrame[] = img.getSpriteFrames();
        return FrameAnimation.crea_frame_animation(imgs, strFraIdArr);
    }

}   
